/*
  Liceses...
*/


#include "morphology.h"

void InitImg(char* FileName, CImg* outimg)
{
	CBitmap bmp;
	bmp.Load(FileName);
	outimg->width = bmp.GetWidth();
	outimg->height = bmp.GetHeight();
	outimg->size = bmp.m_BitmapSize;
	outimg->buff = new pixel_type[outimg->size];
	//memcpy((pixel_type*)outimg->buff, (pixel_type*)bmp.m_GrayscaleBitmapData, outimg->size);
	for(int i = 0; i < outimg->size; i++) outimg->buff[i] = bmp.m_GrayscaleBitmapData[i]; //Ouch!!!!!!!!!
}

void InitPattern(char* FileName, CPattern* outimg)
{
	InitImg(FileName, outimg);
	ulong len1 = 0, len2 = 0;
	for(ulong i = 0; i < outimg->size; i++) 
		if(!outimg->buff[i]) len1++;
	len2 = outimg->size - len1;
	outimg->ind1 = new CPoint[len1];
	outimg->ind2 = new CPoint[len2];
	outimg->len1 = len1;
	outimg->len2 = len2;
	ulong width = outimg->width;
	ulong height = outimg->height;
	len1 = 0;
	len2 = 0;
	for(ulong i = 0; i < width; i++)
	{
		for(ulong j = 0; j < height; j++)
		{
			if(!outimg->buff[ j * width + i ])
			{
				outimg->ind1[len1].x = i;
				outimg->ind1[len1].y = j;
				len1++;
			}
			else
			{
				outimg->ind2[len2].x = i;
				outimg->ind2[len2].y = j;
				len2++;
			}
		}
	}
}

void DestroyImg(CImg* img)
{
	delete [] img->buff;
}

void DestroyPattern(CPattern* pattern)
{
	delete [] pattern->buff;
	delete [] pattern->ind1;
	delete [] pattern->ind2;
}

void InitFindRes(CFindRes* fres,ulong size)
{
	fres->buffsize = size;
	fres->numres = 0;
	fres->res_dist = new double[size];
	fres->res_pos = new CPoint[size];
}

void DestroyFindRes(CFindRes* fres)
{
	fres->buffsize = 0;
	delete [] fres->res_dist;
	delete [] fres->res_pos;
}